package com.smit.livevideo.sample.controller;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import com.smit.dvb.CIData;
import com.smit.dvb.ci.CIControl;
import com.smit.dvb.ci.ICICallback;
import com.smit.livevideo.sample.dialog.CIMMIMenuDialog;
import com.smit.livevideo.sample.dialog.DialogFragmentUtil;
import com.smit.livevideo.sample.util.SDKAPIUtil;
import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/12/24.
 * CI MMI控制类
 */
public class CIMMIController implements ICICallback, Handler.Callback {
    private static final String TAG = CIMMIController.class.getSimpleName();
    private Handler msgHandler;
    private Activity activity;
    private boolean isServicesSwitch = false;

    public static CIMMIController getInstance() {
        return SingleTonHolder.instance;
    }

    /**
     * 初始化CI MMI 控制器的数据
     * @param activity activity
     */
    public void initCIMMIController(Activity activity) {
        LogUtil.debug(TAG, "initCIMMIController~~~");
        this.activity = activity;
        msgHandler = new Handler(this);
        SDKAPIUtil.getCIControl().addCallback(this);
    }

    /**
     * 释放CI MMI控制器的数据
     */
    public void freeCIMMIController() {
        LogUtil.debug(TAG, "freeCIMMIController~~~");
        this.activity = null;
        msgHandler = null;
        SDKAPIUtil.getCIControl().removeCallback(this);
    }

    /**
     * 获取智能卡、固件版本、tuner、dongle相关信息
     */
    public static void getSomeInfo() {
        CIData ciData = SDKAPIUtil.getCIControl().getSmartCardInfo();
        LogUtil.trace(TAG, "");
        LogUtil.trace(TAG, "start getSmartCardInfo>>>");
        LogUtil.trace(TAG, "smartCardStatus:" + ciData.smartCardStatus);
        LogUtil.trace(TAG, "deviceId:" + ciData.deviceId);
        LogUtil.trace(TAG, "deviceType:" + ciData.deviceType);
        LogUtil.trace(TAG, "deviceModel:" + ciData.deviceModel);
        LogUtil.trace(TAG, "firmwareVersion:" + ciData.firmwareVersion);
        LogUtil.trace(TAG, "smartCardId:" + ciData.smartCardId);
        LogUtil.trace(TAG, "tunerType:" + ciData.tunerType);
        LogUtil.trace(TAG, "isDTCheck:" + ciData.isDTCheck);
        LogUtil.trace(TAG, "end getSmartCardInfo>>>");
        ciData = SDKAPIUtil.getCIControl().getHardwareInfo();
        LogUtil.trace(TAG, "");
        LogUtil.trace(TAG, "start getHardwareInfo>>>");
        LogUtil.trace(TAG, "smartCardStatus:" + ciData.smartCardStatus);
        LogUtil.trace(TAG, "deviceId:" + ciData.deviceId);
        LogUtil.trace(TAG, "deviceType:" + ciData.deviceType);
        LogUtil.trace(TAG, "deviceModel:" + ciData.deviceModel);
        LogUtil.trace(TAG, "firmwareVersion:" + ciData.firmwareVersion);
        LogUtil.trace(TAG, "smartCardId:" + ciData.smartCardId);
        LogUtil.trace(TAG, "tunerType:" + ciData.tunerType);
        LogUtil.trace(TAG, "isDTCheck:" + ciData.isDTCheck);
        LogUtil.trace(TAG, "end getHardwareInfo>>>");
        String deviceId = SDKAPIUtil.getCIControl().getDeviceId();
        LogUtil.trace(TAG, "");
        LogUtil.trace(TAG, "start getDeviceId>>>");
        LogUtil.trace(TAG, "deviceId:" + deviceId);
        LogUtil.trace(TAG, "end getDeviceId>>>");
        String deviceType = SDKAPIUtil.getCIControl().getDeviceType();
        LogUtil.trace(TAG, "");
        LogUtil.trace(TAG, "start getDeviceType>>>");
        LogUtil.trace(TAG, "deviceType:" + deviceType);
        LogUtil.trace(TAG, "end getDeviceType>>>");
    }

//    public void setServicesSwitch(boolean servicesSwitch) {
//        this.isServicesSwitch = servicesSwitch;
//    }
//
//    public boolean isServicesSwitch() {
//        return this.isServicesSwitch;
//    }

    /**
     * CI回调接口
     * @param i 消息值
     * @param o 数据
     */
    @Override
    public void handleCICallbackMsg(int i, Object o) {
        LogUtil.debug(TAG, "handleCICallbackMsg:" + i + " o:" + o);
        Message handlerMsg = msgHandler.obtainMessage(i, o);
        handlerMsg.sendToTarget();
    }

    /**
     * 消息处理函数
     * @param msg 消息
     * @return boolean变量
     */
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case CIControl.MSG_CI_DATA_UPDATED:
                LogUtil.debug(TAG, "MSG_CI_DATA_UPDATED!!!");
                getSomeInfo();
                break;
            case CIControl.MSG_CI_SHOW_MMI_MENU:
                if (activity != null) {
                    DialogFragmentUtil.showDialog(activity.getFragmentManager(),
                            DialogFragmentUtil.getCIMMIMenuDialog(), DialogFragmentUtil.CI_MMI_MENU_DIALOG);
                }
                break;
            case CIControl.MSG_CI_UPDATE_MMI_MENU:
                if (DialogFragmentUtil.peekDialogFragment() == DialogFragmentUtil.getCIMMIMenuDialog()) {
                    ((CIMMIMenuDialog) DialogFragmentUtil.getCIMMIMenuDialog()).msgUpdateCIMMIMenu();
                }
                break;
            case CIControl.MSG_CI_HIDE_MMI_MENU:
                if (activity != null) {
                    DialogFragmentUtil.dismissDialogByTag(activity.getFragmentManager(),
                            DialogFragmentUtil.CI_MMI_MENU_DIALOG);
                }
                break;
            case CIControl.MSG_CI_SHOW_PIN_DIALOG:
                if (activity != null) {
                    DialogFragmentUtil.showDialog(activity.getFragmentManager(),
                            DialogFragmentUtil.getCIMMIPinDialog(), DialogFragmentUtil.CI_MMI_PIN_DIALOG);
                }
                break;
            case CIControl.MSG_CI_HIDE_PIN_DIALOG:
                if (activity != null) {
                    DialogFragmentUtil.dismissDialogByTag(activity.getFragmentManager(),
                            DialogFragmentUtil.CI_MMI_PIN_DIALOG);
                }
                break;
            default:
                break;
        }
        return false;
    }

    private static class SingleTonHolder {
        private static CIMMIController instance = new CIMMIController();
    }

}
